<?php

namespace App\Admin\Controllers;

use App\Models\StartUpLogo;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class StartUpController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('启动页管理');
            $content->description('');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('启动页编辑');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新建启动页');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(StartUpLogo::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->images('封面图')->fancyImage('',100,100);
            $states = [
                'on' => ['value' => 1, 'text' => '开启', 'color' => 'primary'],
                'off' => ['value' => 2, 'text' => '关闭', 'color' => 'default'],
            ];
            $grid->enable('状态')->switch($states);

            $grid->order('排序')->editable('select', [
                1 => '1',
                2 => '2',
                3 => '3',
                4 => '4',
                5 => '5',
                6 => '6',
                7 => '7',
                8 => '8',
                9 => '9',
            ]);

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(StartUpLogo::class, function (Form $form) {

            $form->display('id', 'ID');
            //封面图
            $form->image('images', '图片')->rules('required');
            $form->text('url', '跳转连接');
            $form->select('order', '优先级')->options([
                1 => '1',
                2 => '2',
                3 => '3',
                4 => '4',
                5 => '5',
                6 => '6',
                7 => '7',
                8 => '8',
                9 => '9',
            ])->rules('required');

            $states = [
                'on' => ['value' => 1, 'text' => '开启', 'color' => 'primary'],
                'off' => ['value' => 2, 'text' => '关闭', 'color' => 'default'],
            ];
            $form->switch('enable', '是否可用')->states($states);
            $form->textarea('remark', '备注');


        });
    }
}
